package com.siruier.boss.ui.activity.opencard

import android.view.View
import com.siruier.boss.api.core.launchUI
import com.siruier.boss.api.core.toData
import com.siruier.boss.api.kaiKa
import com.siruier.boss.bean.City
import com.siruier.boss.bean.NumberListVO
import com.siruier.boss.databinding.ActivityOpenCardSubmitBinding
import com.siruier.boss.ui.api.ApiObserver
import com.siruier.boss.ui.base.BaseActivity
import com.siruier.boss.ui.ext.toastMessageLong
import com.siruier.boss.ui.ext.value
import com.siruier.boss.ui.helper.InjectBundle
import com.siruier.boss.ui.utils.variableView

class OpenCardSubmitActivity : BaseActivity<ActivityOpenCardSubmitBinding>() {
    private val city: City by InjectBundle()
    private val number: NumberListVO by InjectBundle()
    private var jdProvince: City? = null
    private var jdCity: City? = null

    override fun initView() {
        bindViewClick(vb.llSelectJdCity, vb.buttonSubmit)
        vb.tvNumber.text = number.mobileno

        variableView {
            addTextView(vb.etIdCardNo)
            addTextView(vb.etRealName)
            addTextView(vb.etIdPhone)
            addTextView(vb.etDetailAddress)
            addTextView(vb.tvJdCity)
            addOnValidationResultListener(vb.buttonSubmit)
        }
    }

    override fun onClick(v: View?) {
        when (v) {
            vb.llSelectJdCity -> {
                launchStartActivityForResult(OpenCardCitySelectActivity::class.java, options = {
                    putBoolean("isJdCity", true)
                }) {
                    jdProvince = it.data!!.getParcelableExtra("province")
                    jdCity = it.data!!.getParcelableExtra("city")
                    vb.tvJdCity.text = "${jdProvince!!.name} ${jdCity!!.name}"
                }
            }
            vb.buttonSubmit -> {
                launchUI({
                    kaiKa.checkAddress(jdProvince!!.code, jdCity!!.code, vb.etDetailAddress.value, number.mobileno).toData()
                    kaiKa.order(
                        vb.etDetailAddress.value, jdCity!!.code, vb.etIdCardNo.value, city.code, jdProvince!!.code,
                        number.mobileno, vb.etIdPhone.value, vb.etRealName.value
                    ).toData()
                    "下单成功".toastMessageLong()
                    setResult(RESULT_OK)
                    finish()
                }, ApiObserver(this))
            }
        }
    }
}